﻿using System;
using System.Linq;
using eAtlantida.Tools.Generator;
using eAtlantida.Tools.Generator.Sql;

namespace eAtlantida.Tools.Operations.GenerateOperations
{
    public class GenerateBLLOperation : Operation
    {
        public const string Name = "generate bll";

        public override void Execute(State state)
        {
            var name = state.CommandParameters[0];
            var parameters = GetGenerateParameters(state.CommandParameters.Skip(1));

            WriteMessage("Generating DAL clases...", MessageType.Default);

            var discover = new Discover(state.ConnectionString);
            var generator = new BLLGenerator(
                discover.GetColumns(name), name, parameters.IsCreate, parameters.IsUpdate, parameters.IsDelete, parameters.IsListing, parameters.IsSelect, parameters.IsValidate);
            var basePath = "BLL/Services/" + name;

            try
            {
                AddFile(basePath, SingleName(name), generator.GenerateDomen());
                WriteMessage("Added domen", MessageType.Info);
            }
            catch (Exception ex)
            {
                WriteMessage("Not added domen: " + ex.Message, MessageType.Error);
            }
            
            if (parameters.IsListing)
            {
                try
                {
                    AddFile(basePath + "/Listing", name + "Listing", generator.GenerateListing());
                    WriteMessage("Added listing model", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added listing model: " + ex.Message, MessageType.Error);
                }

                try
                {
                    AddFile(basePath + "/Listing", name + "Filter", generator.GenerateFilter());
                    WriteMessage("Added filter model", MessageType.Info);
                }
                catch (Exception ex)
                {
                    WriteMessage("Not added filter model: " + ex.Message, MessageType.Error);
                }
            }

            try
            {
                AddFile(basePath, name + "Collection", generator.GenerateCollection());
                WriteMessage("Added collection", MessageType.Info);
            }
            catch (Exception ex)
            {
                WriteMessage("Not added collection: " + ex.Message, MessageType.Error);
            }

            try
            {
                AddFile(basePath, name + "Service", generator.GenerateService());
                WriteMessage("Added service", MessageType.Info);
            }
            catch (Exception ex)
            {
                WriteMessage(" Not added service: " + ex.Message, MessageType.Error);
            }

            try
            {
                AddFile(basePath, "I" + name + "Service", generator.GenerateServiceInterface());
                WriteMessage("Added service interface", MessageType.Info);
            }
            catch (Exception ex)
            {
                WriteMessage("Not added service interface: " + ex.Message, MessageType.Error);
            }
        }
    }
}